package io.modifier.basic.core.data.kvCache.property

import io.modifier.basic.core.data.kvCache.IKVOwner
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class KVProperty<V>(
    internal val genKeyPrefix: () -> String,
    internal val decode: (String) -> V,
    internal val encode: Pair<String, V>.() -> Boolean
) : ReadWriteProperty<IKVOwner, V> {
    override fun getValue(thisRef: IKVOwner, property: KProperty<*>): V =
        decode(genKeyPrefix() + property.name)

    override fun setValue(thisRef: IKVOwner, property: KProperty<*>, value: V) {
        encode((genKeyPrefix() + property.name) to value)
    }
}
